<?php

namespace app\admin\controller\official;

use app\admin\library\Brand as Brandserver;
use app\common\controller\Backend;
use app\common\model\User as UserModel;
use fast\Tree;
use think\Db;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    public function imports(){
        return parent::imports();
    }
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
			$conn = Db::connect(config('database.pajk'));
            $total = $conn->name('user')
                ->where($where)
                ->order($sort, $order)
                ->field('id')
                ->count();
            $list = $conn->name('user')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->field('id,last_login,mobile,login_num,last_ip,openid,create_time')
                ->select();
			//判断有没有订购年卡
			$paytime = strtotime("-1 year");
			foreach($list as $key => $value){
				$list[$key]['is_member'] = 0;
				$list[$key]['product'] = '';
				
				$orderInfo = $conn->name('order')->where('mobile',$value['mobile'])->where('order_status',2)->where('pay_time','>',$paytime)->find();
				if($orderInfo){
					$list[$key]['is_member'] = 1;
					$list[$key]['product'] = $orderInfo['interests_name'];
				}else{
					$startTime = strtotime(date('Y-m'));
					$endTime = mktime(23,59,59,date('m',strtotime(date('Y-m')))+1,00);  //指定月份月末时间戳 
					$isFind = $conn->name('callback')->where('server_num',$value['mobile'])->where('add_time','between',[$startTime,$endTime])->find();
					if($isFind){
						$list[$key]['is_member'] = 1;
						$list[$key]['product'] = $isFind['sku_name'];
					}
				}
			}
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
}
